<?php
namespace app\middleware;

use think\facade\Db;
use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;

class Init implements MiddlewareInterface
{
    public function process(Request $request, callable $handler) : Response
    {
        $file=base_path().'/tpl/'.'install.txt';
        if(!file_exists($file) and "app\controller\IndexController"==$request->controller){
            return redirect('/install');
        }

        if($request->app=='' and "app\controller\InstallController"!=$request->controller and ""!=$request->controller){
            $system= Db::name('system')->column('value','enname');
            if($system["is_open"] != "开启"){
                if($system["redirect"]){
                    return redirect($system["redirect"]);
                }
                e('非法访问');
            }
        }

        $response = $handler($request); // 继续向洋葱芯穿越，直至执行控制器得到响应
        return $response;
    }
}